package com.inforich.projectManage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.inforich.common.constant.ProjectConstants;
import com.inforich.common.core.domain.AjaxResult;
import com.inforich.common.enums.ApproveEnum;
import com.inforich.projectManage.domain.ProjectRubbishExpand;
import com.inforich.projectManage.domain.ProjectRubbishRemoval;
import com.inforich.projectManage.service.IProjectRubbishExpandService;
import com.inforich.projectManage.service.IProjectRubbishRemovalService;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inforich.projectManage.mapper.ProjectMapper;
import com.inforich.projectManage.domain.Project;
import com.inforich.projectManage.service.IProjectService;

/**
 * 项目Service业务层处理
 *
 * @author hjm
 * @date 2022-10-22
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements
    IProjectService {

  @Autowired
  private IProjectRubbishExpandService projectRubbishExpandService;
  @Autowired
  private IProjectRubbishRemovalService projectRubbishRemovalService;


  @Override
  public Page<Project> pageProject(Page<Project> page, Map<String, Object> map) {
    return baseMapper.pageProject(page, map);
  }

  @Override
  public List<Project> listProject(Map<String, Object> map) {
    return baseMapper.listProject(map);
  }

  @Override
  public Project getProjectById(String id) {
    return baseMapper.selectProjectById(id);
  }

  @Override
  public int insertProject(Project project) {
    return baseMapper.insertProject(project);
  }

  @Override
  public int updateProject(Project project) {
    return baseMapper.updateProject(project);
  }

  @Override
  public String getProjectName(String id) {
    Project project = getProjectById(id);
    return project == null ? "" : project.getName();
  }

  @Override
  public String checkData(Project project) {
    int count = count(new LambdaQueryWrapper<Project>()
        .eq(Project::getName, project.getName())
        .ne(StrUtil.isNotBlank(project.getId()), Project::getId, project.getId()));
    return count > 0 ? "项目名称重复" : "";
  }

  @Override
  public AjaxResult approveResource(JSONObject param) {
    String id = param.getString("id");
    String status = param.getString("status");
    String resourceExplain = param.getString("resourceExplain");
    if (StrUtil.hasBlank(id, status, resourceExplain)) {
      return AjaxResult.error("参数缺失");
    }
    Long resourceState = "1".equals(status) ? 30L : 40L;
    boolean result = update(new LambdaUpdateWrapper<Project>()
        .set(Project::getResourceState, resourceState)
        .set(Project::getResourceExamineTime, new Date())
        .set(Project::getResourceExplain, resourceExplain)
        .eq(Project::getId, id)
        .eq(Project::getResourceState, 20));
    return result ? AjaxResult.success() : AjaxResult.error();
  }

  @Override
  public AjaxResult approveProject(JSONObject param) {
    String id = param.getString("id");
    String status = param.getString("status");
    if (StrUtil.hasBlank(id, status)) {
      return AjaxResult.error("参数缺失");
    }
    Long approvalStatus = "1".equals(status) ? 20L : 30L;
    boolean result = update(new LambdaUpdateWrapper<Project>()
        .set(Project::getApprovalStatus, approvalStatus)
        .eq(Project::getId, id)
        .eq(Project::getApprovalStatus, 10));
    return result ? AjaxResult.success() : AjaxResult.error();
  }

  @Override
  public Boolean checkProjectPass(String projectId) {
    if (StrUtil.isBlank(projectId)) {
      return false;
    }
    Project project = getProjectById(projectId);
    if (project == null) {
      return false;
    }
    Long projectKindId = project.getProjectKindId();
    int count = 0;
    if (ProjectConstants.PROJECT_KIND_EXPAND.equals(projectKindId)) {
      count = projectRubbishExpandService.count(new LambdaQueryWrapper<ProjectRubbishExpand>()
          .eq(ProjectRubbishExpand::getProjectId, projectId)
          .eq(ProjectRubbishExpand::getApprovalStatus, ApproveEnum.PASS.getDbData()));
      return count > 0;
    } else if (ProjectConstants.PROJECT_KIND_RENOVATION.equals(projectKindId)) {
      count = projectRubbishExpandService.count(new LambdaQueryWrapper<ProjectRubbishExpand>()
          .eq(ProjectRubbishExpand::getProjectId, projectId)
          .eq(ProjectRubbishExpand::getApprovalStatus, ApproveEnum.PASS.getDbData()));
      return count > 0;
    } else if (ProjectConstants.PROJECT_KIND_REMOVAL.equals(projectKindId)) {
      count = projectRubbishRemovalService.count(new LambdaQueryWrapper<ProjectRubbishRemoval>()
          .eq(ProjectRubbishRemoval::getProjectId, projectId)
          .eq(ProjectRubbishRemoval::getApprovalStatus, ApproveEnum.PASS.getDbData()));
      return count > 0;
    } else {
      return false;
    }
  }


}
